/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\msgtranslator.h"

#include "MMachineInfo.h"

//---------------------------------------------------------------------------

MsgTranslator::MsgTranslator(void)
{
}

MsgTranslator::~MsgTranslator(void)
{
}

//---------------------------------------------------------------------------

CString MsgTranslator::GetString(int id)
{
	CString str;

	switch(id)
	{
		case FAILED_TO_CREATE_USER_INTERFACE :
			str  = _T("Creation of User Interface has failed !\n");
			str += _T("The application will now close.");
			break;

		case UNABLE_TO_LOAD_THE_SKIN :
			str = _T("Unable to load the skin !");
			break;

		case UNABLE_TO_LOAD_CONFIGURATION :
			str  = _T("Failed to load the configuration file!\n");
			str += _T("The default value will be loaded and saved.");
			break;

		case UNABLE_TO_SAVE_CONFIGURATION :
			str = _T("Unable to save the configuration file !");
			break;

		case COULD_NOT_LOAD_VENDORS :
			str = _T("Failed to load the \"vendors.dat\" file!");
			break;

		case COULD_NOT_LOAD_MCAST4 :
			str = _T("Failed to load the \"mcast4.dat\" file!");
			break;

		case COULD_NOT_LOAD_PORTASSIGLIST:
			str = _T("Failed to load the \"ports.dat\" file!");
			break;

		case SCAN_FAILED :
			str = _T("Scan failed !");
			break;

		case SCAN_DID_NOT_FINISH_CLEAN :
			str = _T("The scanning process did not finish cleanly !");
			break;

		case STOP_COMMAND_HAS_BEEN_SENT :
			str = _T("     - The STOP command has been sent.");
			break;

		case LAYOUT_RESET :
			str = _T("Layout has been reset.");
			break;
		case LAYOUT_RESET_FAILED :
			str = _T("Layout has NOT been reset correctly !");
			break;

		case MAC_ADDRESS_WRONG_SPEC :
			str = _T("Invalid MAC specification !");
			break;

		case MCAST4_ADDRESS_WRONG_SPEC :
			str = _T("Invalid multicast IPv4 address specification !");
			break;

		case CONFIGURATION_UPDATED :
			str = _T("General configuration has been updated.");
			break;

		default:
			str = _T("?");
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

CString MsgTranslator::GetStringWithParam(int msg, int id)
{
	CString str;

	if(msg!=SCAN_FAILED){
		str = _T("Invalid message !");
		return str;
	}

	str.Empty();
	switch(id){
		case MSR_ERR_SCAN_IN_PROGRESS :
			str = _T("Scan failed because another scan session is in progress !");
			break;
		case MSR_ERR_PARAMETER :
			str = _T("Scan failed because of an invalid parameter !");
			break;
		case MSR_ERR_FAILED :
			str = _T("Scan failed (memory allocation error ?) !");
			break;
		case MSR_ERR_GW_MAC_NOT_FOUND :
			str = _T("Scan failed because the MAC address for the default gateway was not found !");
			break;
		case MSR_ERR_GET_IP_INTERFACE :
			str = _T("Scan failed because I was unable to get an IP interface !");
			break;

		default:
			str.Format(_T("Scan failed with the error %d !"), id);
			break;
	}

	return str;
}

CString MsgTranslator::GetStringWithParamS(int msg, CString strIn)
{
	CString str;

	switch(msg){
		case COULD_NOT_READ_CONFIG_FILE :
			str.Format(_T("Could not read scan configuration file %s !"), strIn);
			break;
		case COULD_NOT_LOAD_CONFIG_FILE :
			str.Format(_T("Invalid scan configuration file %s !"), strIn);
			break;
		case CONFIG_FILE_LOADED :
			str.Format(_T("Loaded scan configuration %s."), strIn);
			break;

		case MCAST4_ADDRESS_ASSIGNED_NF :
			str.Format(_T("Unknown assignment for %s."), strIn);
			break;

		default :
			str.Format(_T("%d: %s !"), msg, strIn);
			break;
	}

	return str;
}

CString MsgTranslator::GetStringWithParamSS(int msg, CString strIn1, CString strIn2)
{
	CString str;

	switch(msg){
		case MCAST4_ADDRESS_ASSIGNED :
			str.Format(_T("The %s multicast address is assigned for \"%s\"."), strIn1, strIn2);
			break;

		default :
			str.Format(_T("%d: %s - %s!"), msg, strIn1, strIn2);
			break;
	}

	return str;
}
//---------------------------------------------------------------------------

CString MsgTranslator::GetStringForMsgScanStart(WPARAM wp, LPARAM lp)
{
	CString str;

	switch(wp){
		case 0 :
			str = _T("Starting the scan process...");
			break;
		case 1 :
			str = _T("Started the passive scan process...");
			break;
		case 2 :
			str = _T("Started the hunt mode scan process...");
			break;
		default:
			str.Format(_T("Starting the %d scan process..."), wp);
			break;
	}

	return str;
}

CString MsgTranslator::GetStringForMsgScanDone(WPARAM wp, LPARAM lp)
{
	CString str;

	switch(wp){
		case 0 :
			str.Format(_T("... scan finished in %s."), GetTime((DWORD)lp));
			break;
		case 1 :
			str.Format(_T("... passive scan finished after %s."), GetTime((DWORD)lp));
			break;
		default:
			str.Format(_T("... %d scan finished after %s."), wp, GetTime((DWORD)lp));
			break;
	}

	return str;
}

//---------------------------------------------------------------------------
CString MsgTranslator::GetStringForAbortReason(WPARAM wp)
{
	CString str;

	switch(wp){
		case MSR_ERR_PARAMETER :
			str = _T("incorrect parameter was supplied.");
			break;

		case MSR_ERR_PHASE :
			str = _T("does not work on assigned phase.");
			break;

		case MSR_ERR_PRIV_ELEVATION :
			str = _T("has failed to elevate privileges.");
			break;

		case MSR_ERR_DNS_MAC_NOT_FOUND :
			str = _T("DNS server could not be contacted.");
			break;

		default:
			str.Format(_T("code %d"), wp);
			break;
	}

	return str;
}

CString MsgTranslator::GetStringForMessage(int id, WPARAM wp, LPARAM lp)
{
	MMachineInfo *mach;
	CString str;
	int sw;

	switch(id)
	{
		case CHS_MSG_SCAN_START :
			str = GetStringForMsgScanStart(wp, lp);
			break;

		case CHS_MSG_SCAN_DONE :
			// lpIn is elapsed time
			str = GetStringForMsgScanDone(wp, lp);
			break;

		case CHS_MSG_SCAN_SKIPPED :
			// lpIn is elapsed time
			str = _T("Scanning is skipped because the address count is zero.");
			break;

		case CHS_MSG_SCAN_RANDOMIZE_ADDRLIST :
			// wpIn = 0, lpIn = 0
			str = _T("     - The list of addresses is randomized.");
			break;

		case CHS_MSG_SCAN_SORT_ADDRLIST :
			// wpIn = 0, lpIn = 0
			str = _T("     - The list of addresses is sorted.");
			break;

		case CHS_MSG_SCANPHASE_START :
			// wpIn is WorkPhaseType
			str.Format(_T("     - Starting the %s phase"), wds.GetPhaseName((WorkPhaseType)wp));
			break;

		case CHS_MSG_SCANPHASE_DONE :
			// wpIn is WorkPhaseType
			str.Format(_T("     - The %s phase is done."), wds.GetPhaseName((WorkPhaseType)wp));
			break;

		case CHS_MSG_SCANPHASE_FAILED :
			// wpIn is WorkPhaseType
			str.Format(_T("     - The %s phase has failed !"), wds.GetPhaseName((WorkPhaseType)wp));
			break;

		case CHS_MSG_SCANPHASE_SKIPPED :
			// wpIn is WorkPhaseType
			str.Format(_T("     - The %s phase is skipped."), wds.GetPhaseName((WorkPhaseType)wp));
			break;

		case CHS_MSG_AFTER_PHASE :
			// lpIn is the number of addresses
			sw = (int)wp;
			switch(sw){
				case 0 :
					str.Format(_T("     - After MAC Discovery address count is %u."), (unsigned int)lp);
					break;
				case 2 :
					str.Format(_T("     - After Discovery address count is %u."), (unsigned int)lp);
					break;
				default :
					str.Format(_T("     - After \"%d\" address count is %u."), sw, (unsigned int)lp);
					break;
			}
			break;

		case CHS_MSG_LIST_BUILDED :
			// lpIn is the number of items
			sw = (int)wp;
			switch(sw){
				case 0 :
					// addresses
					str.Format(_T("     - Builded the list of addresses. The count is %u."), (unsigned int)lp);
					break;
				case 1 :
					// addresses
					str.Format(_T("     - Builded the list of ports. The count is %u."), (unsigned int)lp);
					break;
				case 2 :
					// addresses
					str.Format(_T("     - Builded the list of protocols. The count is %u."), (unsigned int)lp);
					break;
				default:
					str.Format(_T("     - Builded the list of \"%d\". The count is %u."), sw, (unsigned int)lp);
					break;
			}
			break;

		case CHS_MSG_PACKETS_SENT :
			// wpIn = 0, lpIn = number
			// wpIn = 1 - WOL, lpIn = number
			sw = (int)wp;
			switch(wp){
				case 1 :
					// WOL
					str.Format(_T("     - WakeOnLAN sent packets count is %d"), (unsigned int)lp);
					break;
				default:
					str.Format(_T("     - Sent packets count is %d"), (unsigned int)lp);
					break;
			}
			break;

		case CHS_MSG_METHOD_START :
			// wpIn = 0, lpIn = number
			str.Format(_T("          - Method %s"), wds.GetMethodName((WorkMethodType)lp));
			break;

		case CHS_MSG_METHOD_ABORT :
			// wpIn = abort_reason, lpIn = number
			str.Format(_T("          - Method %s aborted because %s"), wds.GetMethodName((WorkMethodType)lp), GetStringForAbortReason(wp));
			break;

		case CHS_MSG_SYSARPTAB_UPDATED :
			// wpIn = 0 - error, 1 - OK, lpIn = unused
			if(wp==0) str = _T("     - FAILED to update system's ARP Table!");
			else      str = _T("     - System's ARP Table has been updated.");
			break;

		case CHS_MSG_HUNT_MACHINE :
			// wpIn = ce face, lpIn = IP Address
			mach = (MMachineInfo*)(void*)lp;
			sw = (int)wp;
			switch(wp){
				case 1 :
					// hunting begin
					if(mach!=NULL)
						str.Format(_T("     - begin hunting of %s"), mach->address.addrIP.GetS());
					break;
				case 2 :
					// hunting done
					if(mach!=NULL)
						str.Format(_T("     - hunting of %s is done"), mach->address.addrIP.GetS());
					break;
				case 3 :
					// hunting done
					if(mach!=NULL)
						str.Format(_T("     - hunting of %s is allready done"), mach->address.addrIP.GetS());
					break;
			}
			break;

		case CHS_MSG_HUNT_INFO :
			sw = (int)wp;
			switch(wp){
				case 1 :
					str = _T("Please wait while I load location data...");
					break;
				case 2 :
					str.Format(_T("... loaded %d IP ranges."), (int)lp);
					break;
				case 3 :
					str = _T("... location data loading failed!");
					break;
			}
			break;

		default:
			str.Format(_T("Message %d - %02x %04x"), id, (WORD)wp, (DWORD)lp);
			break;
	}

	return str;
}

//---------------------------------------------------------------------------

CString MsgTranslator::GetTime(DWORD timeIn)
{
	CString str;

	if(timeIn<1000)
		str = _T("less then a second");
	else
		str.Format(_T("%.3f seconds"), (double)timeIn/1000.0);

	return str;
}
//---------------------------------------------------------------------------
